package com.cgt.springboot.job;

import cn.hutool.core.collection.CollectionUtil;
import com.cgt.springboot.constant.RedisConstants;
import com.cgt.springboot.util.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Set;

@Configuration
public class DeleteImageJob {

	@Autowired
	private RedisTemplate redisTemplate;

	@Scheduled(cron = "0 */2 * * * *")
	public void deleteImage(){
		System.out.println("DeleteImageJob.deleteImage");
		Set<String> set = redisTemplate.opsForSet().difference(RedisConstants.UPLOAD_IMAGE, RedisConstants.UPLOAD_IMAGE_TO_DB);
		if(!CollectionUtils.isEmpty(set)){
			for (String imageName : set) {
				//删除阿里云oss中的图片
				AliOSSUtil.deleteFile(imageName);
				System.out.println("删除图片：" + imageName);
			}
			redisTemplate.delete(RedisConstants.UPLOAD_IMAGE_TO_DB);
			redisTemplate.delete(RedisConstants.UPLOAD_IMAGE);
		}
	}
}
